import {
    teacherLogin,
    getUserInfo,
    updatePassword,
    updateName
} from '@/services/teacher'
import md5 from 'js-md5'

export default {
    namespace: 'teacher',
    state: {
        examListTab: 'examList',
        configOfShowScore: {},
        userInfo: {},
    },
    effects: {
        *teacherLogin({ id, password }, { call, put }) {
            const result = yield call(teacherLogin, { teacher_id: id, teacher_password: md5(password) })
            yield put({
                type: 'beforeChangeState',
                result
            })
            return result
        },

        *getUserInfo({ id }, { call, put }) {
            const result = yield call(getUserInfo, { teach_id: id })
            yield put({
                type: 'beforeChangeState',
                result,
                field: 'userInfo'
            })
            return result
        },

        *updatePassword({ id, prePassword, nextPassword }, { call, put }) {
            const result = yield call(updatePassword, {
                teach_id: id,
                pre_password: md5(prePassword),
                next_password: md5(nextPassword)
            })
            yield put({
                type: 'beforeChangeState',
                result
            })
            return result
        },

        *updateName({ id, nextName }, { call, put }) {
            const result = yield call(updateName, {
                teach_id: id,
                teach_name: nextName
            })
            yield put({
                type: 'beforeChangeState',
                result
            })
            return result
        },

        *beforeChangeState({ result, field }, { put }) {
            if (result.status === 0) {
                if (result.data && field) {
                    yield put({
                        type: 'changeState',
                        result,
                        field
                    })
                }
                if (result.token) {
                    yield put({
                        type: 'token/changeState',
                        token: result.token,
                        ...result.data
                    })
                }
            }
        },

        *changeExamListTab({ examListTab }, { put }) {
            yield put({
                type: 'changeState',
                result: {
                    data: examListTab
                },
                field: 'examListTab'
            })

        }
    },
    reducers: {
        changeState(state, { result, field }) {
            const nextState = JSON.parse(JSON.stringify({ data: state })).data
            nextState[field] = result.data
            return nextState
        },
        changeShowScore(state, { configOfShowScore }) {
            let nextState = JSON.parse(JSON.stringify({ data: state })).data
            nextState = {
                ...nextState,
                configOfShowScore
            }
            console.log(nextState)
            return nextState
        }
    }
}